package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.MchntAcctDetailMapper;
import cc.rengu.igas.bsps.common.entity.InMchntAcctDetail;
import cc.rengu.igas.bsps.common.entity.MchntAcctDetail;
import cc.rengu.igas.bsps.common.entity.OutMchntAcctDetail;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户入账明细表操作接口实现类
 * Created by 王成 on 2018/10/8.
 */
public class MchntAcctDetailMapperImpl implements MchntAcctDetailMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public PageResult selectMchntAcctDetailInfoBySettleDateAndMchntNo(String instId, String mchntNo, String settleStartDate, String settleEndDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAcctDetailInfoBySettleDateAndMchntNo");
        InMchntAcctDetail inMchntAcctDetail = new InMchntAcctDetail();
        inMchntAcctDetail.setInstId(instId);
        inMchntAcctDetail.setStartDate(settleStartDate);
        inMchntAcctDetail.setEndDate(settleEndDate);
        inMchntAcctDetail.setMchntNo(mchntNo);
        return dbsUtil.dbsFetchPageListData(sqlStr, inMchntAcctDetail, OutMchntAcctDetail.class, pageInfo);
    }

    @Override
    public PageResult selectMchntAcctDetailInfoBySettleDateAndMchntNoAndInstId(String instId, String mchntNo, String startDate, String endDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAcctDetailInfoBySettleDateAndMchntNoAndInstId");
        InMchntAcctDetail inMchntAcctDetail = new InMchntAcctDetail();
        inMchntAcctDetail.setInstId(instId);
        if (null != mchntNo && !mchntNo.isEmpty()) {
            inMchntAcctDetail.setMchntNo(mchntNo);
        }
        if (null != startDate && !startDate.isEmpty()) {
            inMchntAcctDetail.setStartDate(startDate);
        }
        if (null != endDate && !endDate.isEmpty()) {
            inMchntAcctDetail.setEndDate(endDate);
        }

        return dbsUtil.dbsFetchPageListData(sqlStr, inMchntAcctDetail, OutMchntAcctDetail.class, pageInfo);
    }

}
